<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2020/1/3
  Time: 11:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<c:if test="${clist eq null}">
    <script>
        location.href="/toadd.do";
    </script>
</c:if>
<body>
<div align="center">
    <h2>添加常见问题</h2>
    <form id="isform">

        <table>
            <tr>
                <td>分类：</td>
                <td>    <select id="classId" name="classId">
                    <option value="-1">--请选择分类--</option>
                    <c:forEach items="${clist}" var="v">
                        <option value="${v.id}">${v.cname}</option>
                    </c:forEach>
                </select></td>
            </tr>
            <tr>
                <td>标题：</td>
                <td><input type="text" id="title" name="title"/><span id="news"></span></td>
            </tr>
            <tr>
                <td>内容:</td>
                <td><textarea cols="30" rows="3" align="center" id="content" name="content"></textarea></td>
            </tr>
        </table>
        <input type="button" value="保存" id="add"/>
        <input type="button" value="放弃"/>
    </form>
</div>
</body>
</html>
<script src="js/jquery-1.12.4.js"></script>
<script>
    $(function () {
        $("#title").change(function () {
            var title = $(this).val();
            $.ajax({
                url : "/check.do",
                type : "post",
                data:{title:title},
                dataType:"json",
                success : function(data) {
                    if (data.result==false){
                        $("#news").html("标题已存在，请重新输入!");

                    }else{
                        $("#news").html("标题可用!");
                    }
                },
                error : function() {
                    alert("错误");
                }
            })
        })
        $("#add").click(function () {
            var classId = $("#classId").val();
            var title = $("#title").val();
            var content = $("#content").val();
            if (classId == "-1") {
                alert("请选择分类！");
                return ;
            }
            if (title == "") {
                alert("请填写标题！");
                return
            }
            if (content == "") {
                alert("请填写内容！");
                return;
            }
            $.ajax({
                url : "/add.do",
                type : "post",
                data:$("#isform").serialize(),
                dataType:"json",
                success : function(data) {
                    if (data.result==true){
                        alert("新增成功！");
                        location.href="/all.do";
                    }else{
                        alert("新增失败！");
                        location.href="/all.do";
                    }
                },
                error : function() {
                    alert("错误");
                }
            })
        })
    })
</script>
